package vashion.azeroth.common.math;

import java.util.Random;

public class RandomUtil {
	//允许获取的最大随机数长度
	@SuppressWarnings("unused")
	private static int MAX_LEN=100;
	//内部随机源，提供深度随机,
	private static final Random RANDOM = new Random();
	/**
	 * @Description:获取随机数
	 * @param len 随机数的长度
	 * @param deep 是否深度随机
	 * @return
	 */
	public static String getRandomStr(int len,boolean deep){
		if(len<1 /*|| len > MAX_LEN*/)
			throw new IllegalArgumentException("Invalid param len:"+len);
		if(!deep)
			return getRandomStr(len);
		char[] buffer = new char[len];
		int gap = 'z' + 1-' ';

		while (len-- != 0) {
			char ch=(char) (RANDOM.nextInt(gap) + ' ');
			if (Character.isDigit(ch)) 
			{
				if(ch >= 56320 && ch <= 57343) {
					if(len == 0) {
						len++;
					} else {
						// low surrogate, insert high surrogate after putting it in
						buffer[len] = ch;
						len--;
						buffer[len] = (char) (55296 + RANDOM.nextInt(128));
					}
				} else if(ch >= 55296 && ch <= 56191) {
					if(len == 0) {
						len++;
					} else {
						// high surrogate, insert low surrogate before putting it in
						buffer[len] = (char) (56320 + RANDOM.nextInt(128));
						len--;
						buffer[len] = ch;
					}
				} else if(ch >= 56192 && ch <= 56319) {
					// private high surrogate, no effing clue, so skip it
					len++;
				} else {
					buffer[len] = ch;
				}
			} else {
				len++;
			}
		}
		return new String(buffer);
	}
	/**
	 * @Description:获取len长度的随机数，浅随机
	 * @param len 随机数的长度
	 * @return
	 */
	public static String getRandomStr(int len){
		if(len<1 /*|| len > MAX_LEN*/)
			throw new IllegalArgumentException("Invalid param len:"+len);
		StringBuffer sb=new StringBuffer();
		for(int i = 1;i<=len;++i)
			sb.append(getRandomInstance().nextInt(10));
		return sb.toString();
	}

	private static final Random getRandomInstance(){
		return new Random();
	}
	
	public static void main(String[] args) {
		System.out.println("生成的10为随机数为：" + getCharAndNumr(10));
	}

   /**
     * java生成随机数字和字母组合
     * @param length[生成随机数的长度]
     * @return
     */
    public static String getCharAndNumr(int length) {
        String val = "";
        Random random = new Random();
        for (int i = 0; i < length; i++) {
        	// 输出字母还是数字
        	String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
        	
        	if ("char".equalsIgnoreCase(charOrNum)) {// 字母
        		// 取得大写字母还是小写字母
        		//int choice = random.nextInt(2) % 2 == 0 ? 65 : 97;
        		int choice = 97;
        		val += (char) (choice + random.nextInt(26));
        	} else if ("num".equalsIgnoreCase(charOrNum)) {// 数字
        		val += String.valueOf(random.nextInt(10));
        	}
        }
        return val;
	}

    /**
      * java生成随机数字组合
      * @param length[生成随机数的长度]
      * @return
      */
     public static String getRandomNumr(int length) {
         String val = "";
         Random random = new Random();
         for (int i = 0; i < length; i++) {
         	val += String.valueOf(random.nextInt(10));
         }
         return val;
 	}
}